#include "..\\stdafx.h"
#include "GdiPaint.h"

MyColor::MyColor(){
    m_lastStyle = -1;
}
MyColor::~MyColor(){
    
}

static String m_systemFont;

void MyColor::setSystemFont(String value){
    m_systemFont = value;
}

String MyColor::getSystemFont(){
    if ((int)m_systemFont.length() == 0)
    {
        m_systemFont = L"Segoe UI";
    }
    return m_systemFont;
}

static int m_style = 0;

int MyColor::getStyle(){
    return m_style;
}
void MyColor::setStyle(int value){
    m_style = value;
}

void MyColor::createColors(){ 
	m_userColors.clear();
	if (m_style == 1)
    {
        m_userColors.put(FCColor_Back, FCColor::rgb(255, 255, 255)); //1
        m_userColors.put(FCColor_Border, FCColor::rgb(200, 200, 200)); //1
        m_userColors.put(FCColor_Text, FCColor::rgb(0, 0, 0)); //1
        m_userColors.put(FCColor_DisabledBack, FCColor::rgba(0, 0, 0, 25)); //1
        m_userColors.put(FCColor_DisabledText, 3289650);
        m_userColors.put(FCColor_Hovered, FCColor::rgba(43, 138, 195, 150));
        m_userColors.put(FCColor_Pushed, FCColor::rgba(43, 138, 195, 100));
    }else{
        m_userColors.put(FCColor_Back, FCColor::rgb(19, 21, 26));
        m_userColors.put(FCColor_Border, FCColor::rgb(50, 50, 50));
        m_userColors.put(FCColor_Text, FCColor::rgb(255, 255, 255));
        m_userColors.put(FCColor_DisabledBack, FCColor::rgba(255, 255, 255, 25));
        m_userColors.put(FCColor_DisabledText, 3289650);
        m_userColors.put(FCColor_Hovered, FCColor::rgba(43, 138, 195, 150));
        m_userColors.put(FCColor_Pushed, FCColor::rgba(43, 138, 195, 100));
    }
}

Long MyColor::getUserColor(Long color){
    if (m_lastStyle != m_style)
    {
        m_lastStyle = m_style;
       createColors();
    }
    if (m_userColors.containsKey(color))
    {
       color = m_userColors.get(color);
    }
    return color;
}

void GdiPaint::affectScaleFactor(FCRect *rect){
		if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
            rect->left = (int)(rect->left * m_scaleFactorX);
            rect->top = (int)(rect->top * m_scaleFactorY);
            rect->right = (int)(rect->right * m_scaleFactorX);
            rect->bottom = (int)(rect->bottom * m_scaleFactorY);
        }
	}

////////////////////////////////////////////////////////////////////////////////////////////////////////////

GdiPaint::GdiPaint(){
	m_hDC = 0;
	m_hRgn = 0;
	m_offsetX = 0;
	m_offsetY = 0;
	m_offsetX2 = 0;
	m_offsetY2 = 0;
	m_pRect.left = 0;
	m_pRect.top = 0;
	m_pRect.right = 0;
	m_pRect.bottom = 0;
	m_rotateAngle = 0;
	m_scaleFactorX = 0;
	m_scaleFactorY = 0;
	m_wRect.left = 0;
	m_wRect.top = 0;
	m_wRect.right = 0;
	m_wRect.bottom = 0;
	m_memBM = 0;
	m_wndHDC = 0;
	m_myColor = new MyColor;
}

GdiPaint::~GdiPaint(){
	if(m_myColor){
		delete m_myColor;
		m_myColor = 0;
	}
	if(m_hDC){
		DeleteDC(m_hDC);
		m_hDC = 0;
	}
	if (m_hRgn){
        DeleteObject(m_hRgn);
        m_hRgn = 0;
    }
	if(m_memBM){
		DeleteObject(m_memBM);
		m_memBM = 0;
	}
	clearCaches();
	m_wndHDC = 0;
}

void GdiPaint::addArc(const FCRect& rect, float startAngle, float sweepAngle){
}

void GdiPaint::addBezier(FCPoint *apt, int cpt){
}

void GdiPaint::addCurve(FCPoint *apt, int cpt){
}

void GdiPaint::addEllipse(const FCRect& rect){
}

void GdiPaint::addLine(int x1, int y1, int x2, int y2){
}

void GdiPaint::addRect(const FCRect& rect){
}

void GdiPaint::addPie(const FCRect& rect, float startAngle, float sweepAngle){
}

void GdiPaint::addText(String strText, FCFont *font, const FCRect& rect, int width){
}

void GdiPaint::beginExport(const String& exportPath, const FCRect& rect){
}

void GdiPaint::beginPaint(HDC hDC, const FCRect& wRect, const FCRect& pRect){
	m_pRect = pRect;
	m_wRect = wRect;
	m_wndHDC = hDC;
	int width = m_wRect.right - m_wRect.left;
	int height = m_wRect.bottom - m_wRect.top;
	m_hDC = CreateCompatibleDC(hDC);
	m_memBM = CreateCompatibleBitmap(hDC, width,  height);
	m_resourcePath = L"";
	SelectObject(m_hDC, m_memBM);
	FCRect rc = {-1, -1, 1, 1};
	FCFont *font = new FCFont;
	drawText(L"", FCColor::rgb(0, 0, 0), font, rc, -1);
	delete font;
	font = 0;
}

void GdiPaint::beginPath(){
}

void GdiPaint::clearCaches(){
}

void GdiPaint::clipPath(){
}

void GdiPaint::closeFigure(){
}

void GdiPaint::closePath(){
}

void GdiPaint::drawArc(Long dwPenColor, float width, int style, const FCRect& rect, float startAngle, float sweepAngle){
}

void GdiPaint::drawBezier(Long dwPenColor, float width, int style, FCPoint *apt, int cpt){
}

void GdiPaint::drawCurve(Long dwPenColor, float width, int style, FCPoint *apt, int cpt){
}

void GdiPaint::drawEllipse(Long dwPenColor, float width, int style, const FCRect& rect){
	drawEllipse(dwPenColor, width, style, rect.left, rect.top, rect.right, rect.bottom);
}

void GdiPaint::drawEllipse(Long dwPenColor, float width, int style, int left, int top, int right, int bottom){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if(dwPenColor < 0 ) dwPenColor = -dwPenColor / 1000;
	HPEN hPen = ::CreatePen(style, (int)width, (int)dwPenColor); 
	HPEN hOldPen = (HPEN)::SelectObject(m_hDC, hPen);
	::SelectObject(m_hDC, ::GetStockObject(HOLLOW_BRUSH));
	FCRect newRect = {left + m_offsetX, top + m_offsetY, right + m_offsetX, bottom + m_offsetY};
	affectScaleFactor(&newRect);
	::Ellipse(m_hDC, newRect.left, newRect.top, newRect.right, newRect.bottom);
	::SelectObject(m_hDC, hOldPen);
	::DeleteObject(hPen); 
}

void GdiPaint::drawImage(String imagePath, const FCRect& rect){
}

void GdiPaint::drawLine(Long dwPenColor, float width, int style, const FCPoint& x, const FCPoint& y){
	drawLine(dwPenColor, width, style, x.x, x.y, y.x, y.y);
}

void GdiPaint::drawLine(Long dwPenColor, float width, int style, int x1, int y1, int x2, int y2){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if(dwPenColor < 0 ) dwPenColor = -dwPenColor / 1000;
	HPEN hPen = ::CreatePen(style, (int)width, (int)dwPenColor); 
	HPEN hOldPen = (HPEN)::SelectObject(m_hDC, hPen);
	int lx1 = x1 + m_offsetX;
    int ly1 = y1 + m_offsetY;
    int lx2 = x2 + m_offsetX;
    int ly2 = y2 + m_offsetY;
    if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
        lx1 = (int)(m_scaleFactorX * lx1);
        ly1 = (int)(m_scaleFactorY * ly1);
        lx2 = (int)(m_scaleFactorX * lx2);
        ly2 = (int)(m_scaleFactorY * ly2);
    }
	::MoveToEx(m_hDC, lx1, ly1, 0); 
	::LineTo(m_hDC, lx2, ly2); 
	::SelectObject(m_hDC, hOldPen);
	::DeleteObject(hPen); 
}

void GdiPaint::drawPath(Long dwPenColor, float width, int style){
}

void GdiPaint::drawPie(Long dwPenColor, float width, int style, const FCRect& rect, float startAngle, float sweepAngle){
}

void GdiPaint::drawPolygon(Long dwPenColor, float width, int style, FCPoint *apt, int cpt){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if(dwPenColor < 0 ) dwPenColor = -dwPenColor / 1000;
	for(int i = 0; i< cpt; i++){
		int x = apt[i].x + m_offsetX;
		int y = apt[i].y + m_offsetY;
		if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
			x = (int)(m_scaleFactorX * x);
			y = (int)(m_scaleFactorY * y);
		}
		apt[i].x = x;
		apt[i].y = y;
	}
	HPEN hPen = ::CreatePen(style, (int)width, (int)dwPenColor); 
	HPEN hOldPen = (HPEN)::SelectObject(m_hDC, hPen);
	::Polygon(m_hDC,apt,cpt);
	::SelectObject(m_hDC, hOldPen);
	::DeleteObject(hPen); 
}

void GdiPaint::drawPolyline(Long dwPenColor, float width, int style, FCPoint *apt, int cpt){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if(dwPenColor < 0 ) dwPenColor = -dwPenColor / 1000;
	for(int i = 0; i< cpt; i++){
		int x = apt[i].x + m_offsetX;
		int y = apt[i].y + m_offsetY;
		if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
			x = (int)(m_scaleFactorX * x);
			y = (int)(m_scaleFactorY * y);
		}
		apt[i].x = x;
		apt[i].y = y;
	}
	HPEN hPen = ::CreatePen(style, (int)width, (int)dwPenColor); 
	HPEN hOldPen = (HPEN)::SelectObject(m_hDC, hPen);
	::Polyline(m_hDC, apt, cpt);
	::SelectObject(m_hDC, hOldPen);
	::DeleteObject(hPen); 
}

void GdiPaint::drawRect(Long dwPenColor, float width, int style, int left, int top, int right, int bottom){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if(dwPenColor < 0 ) dwPenColor = -dwPenColor / 1000;
	HPEN hPen = ::CreatePen(style, (int)width, (int)dwPenColor);
	HPEN hOldPen = (HPEN)::SelectObject(m_hDC, hPen);
	::SelectObject(m_hDC, ::GetStockObject(HOLLOW_BRUSH));
	FCRect newRect = {left + m_offsetX, top + m_offsetY, right + m_offsetX, bottom + m_offsetY};
	affectScaleFactor(&newRect);
	::Rectangle(m_hDC, newRect.left, newRect.top, newRect.right, newRect.bottom);
	::SelectObject(m_hDC, hOldPen);
	::DeleteObject(hPen);
}

void GdiPaint::drawRect(Long dwPenColor, float width, int style, const FCRect& rect){
	drawRect(dwPenColor, width, style, rect.left, rect.top, rect.right, rect.bottom);
}

void GdiPaint::drawRoundRect(Long dwPenColor, float width, int style, const FCRect& rect, int cornerRadius){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if(dwPenColor < 0 ) dwPenColor = -dwPenColor / 1000;
	HPEN hPen = ::CreatePen(style, (int)width, (int)dwPenColor);
	HPEN hOldPen = (HPEN)::SelectObject(m_hDC, hPen);
	::SelectObject(m_hDC, ::GetStockObject(HOLLOW_BRUSH));
	FCRect newRect = {rect.left + m_offsetX, rect.top + m_offsetY, rect.right + m_offsetX, rect.bottom + m_offsetY};
	affectScaleFactor(&newRect);
	if(cornerRadius != 0){
		::RoundRect(m_hDC, newRect.left, newRect.top, newRect.right, newRect.bottom, cornerRadius, cornerRadius);
	}
	else{
		::Rectangle(m_hDC, newRect.left, newRect.top, newRect.right, newRect.bottom);
	}
	::SelectObject(m_hDC, hOldPen);
	::DeleteObject(hPen);
}

void GdiPaint::drawText(String strText, Long dwPenColor, FCFont *font, const FCRect& rect, int width){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if(dwPenColor < 0 ) dwPenColor = -dwPenColor / 1000;
	float fontSize = (int)font->m_fontSize + 6;
	if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
		fontSize = (float)(fontSize * (m_scaleFactorX + m_scaleFactorY) / 2);
	}
	String fontFamily = font->m_fontFamily;
	if(fontFamily == L"Default"){
		fontFamily = L"Segoe UI";
	}
	HFONT hFont = CreateFont
	(
		fontSize, 0,    
		0, 0,    
		font->m_bold? FW_BOLD: FW_REGULAR,    
		font->m_italic ? 1: 0,
		font->m_underline? 1: 0,
				0,
				GB2312_CHARSET,
				OUT_DEFAULT_PRECIS,
				CLIP_DEFAULT_PRECIS,
				DEFAULT_QUALITY,
				DEFAULT_PITCH | FF_SWISS,  
		fontFamily.c_str()    
	);
	FCRect newRect = {rect.left + m_offsetX, rect.top + m_offsetY, rect.right + m_offsetX, rect.bottom + m_offsetY};
	affectScaleFactor(&newRect);
	::SetBkMode(m_hDC, TRANSPARENT);
	::SetTextColor(m_hDC, (int)dwPenColor);
	HFONT hOldFont = (HFONT)::SelectObject(m_hDC, hFont);
	::DrawText(m_hDC, strText.c_str(), -1, &newRect, 0 | DT_NOPREFIX);
	::SelectObject(m_hDC, hOldFont);
	::DeleteObject(hFont);
}

void GdiPaint::drawText(String strText, Long dwPenColor, FCFont *font, const FCRectF& rect, int width){
	FCRect rc = {(int)rect.left, (int)rect.top, (int)rect.right, (int)rect.bottom};
	drawText(strText, dwPenColor, font, rc, width);
}

void GdiPaint::drawTextAutoEllipsis(String strText, Long dwPenColor, FCFont *font, const FCRect& rect){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if(dwPenColor < 0 ) dwPenColor = -dwPenColor / 1000;
	float fontSize = font->m_fontSize + 6;
	if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
		fontSize = (float)(fontSize * (m_scaleFactorX + m_scaleFactorY) / 2);
	}
	String fontFamily = font->m_fontFamily;
	if(fontFamily == L"Default"){
		fontFamily = L"Segoe UI";
	}
	HFONT hFont = CreateFont
	(
		fontSize, 0,    
		0, 0,    
		font->m_bold? FW_BOLD: FW_REGULAR,    
		font->m_italic ? 1: 0,
		font->m_underline? 1: 0,
				0,
				GB2312_CHARSET,
				OUT_DEFAULT_PRECIS,
				CLIP_DEFAULT_PRECIS,
				DEFAULT_QUALITY,
				DEFAULT_PITCH | FF_SWISS,  
		fontFamily.c_str()    
	);
	FCRect newRect = {rect.left + m_offsetX, rect.top + m_offsetY, rect.right + m_offsetX, rect.bottom + m_offsetY};
	affectScaleFactor(&newRect);
	::SetBkMode(m_hDC, TRANSPARENT);
	::SetTextColor(m_hDC, (int)dwPenColor);
	HFONT hOldFont = (HFONT)::SelectObject(m_hDC, hFont);
	::DrawText(m_hDC, strText.c_str(), -1, &newRect, 0 | DT_NOPREFIX | DT_WORD_ELLIPSIS);
	::SelectObject(m_hDC, hOldFont);
	::DeleteObject(hFont);
}

void GdiPaint::endExport(){
}

void GdiPaint::endPaint(){
	int left = m_pRect.left;
	int top = m_pRect.top;
	int width = m_pRect.right - m_pRect.left;
	int height = m_pRect.bottom - m_pRect.top;
	if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
		left = (int)(m_scaleFactorX * left);
		top = (int)(m_scaleFactorY * top);
		width = (int)(m_scaleFactorX * width);
		height = (int)(m_scaleFactorY * height);
	}
	BitBlt(m_wndHDC, left + m_offsetX2, top + m_offsetY2, width, height, m_hDC, left, top, SRCCOPY);
	if(m_hDC){
		DeleteDC(m_hDC);
		m_hDC = 0;
	}
	if(m_hRgn){
		DeleteObject(m_hRgn);
		m_hRgn = 0;
	}
	if(m_memBM){
		DeleteObject(m_memBM);
		m_memBM = 0;
	}
	m_offsetX = 0;
	m_offsetY = 0;
	m_resourcePath = L"";
	m_wndHDC = 0;
}

void GdiPaint::excludeClipPath(){
}

void GdiPaint::fillEllipse(Long dwPenColor, const FCRect& rect){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if(dwPenColor < 0 ) dwPenColor = -dwPenColor / 1000;
	HBRUSH brush = ::CreateSolidBrush((int)dwPenColor);
	::SelectObject(m_hDC, brush);
	FCRect newRect = {rect.left + m_offsetX, rect.top + m_offsetY, rect.right + m_offsetX, rect.bottom + m_offsetY};
	affectScaleFactor(&newRect);
	::Ellipse(m_hDC, newRect.left, newRect.top, newRect.right, newRect.bottom);
	::DeleteObject(brush);
}

void GdiPaint::fillGradientEllipse(Long dwFirst, Long dwSecond, const FCRect& rect, int angle){
	fillEllipse(dwFirst, rect);
}

void GdiPaint::fillGradientPath(Long dwFirst, Long dwSecond, const FCRect& rect, int angle){
}

void GdiPaint::fillGradientPolygon(Long dwFirst, Long dwSecond, FCPoint *apt, int cpt, int angle){
	fillPolygon(dwFirst, apt, cpt);
}

void GdiPaint::fillGradientRect(Long dwFirst, Long dwSecond, const FCRect& rect, int cornerRadius, int angle){
	if(cornerRadius != 0){
		fillRoundRect(dwFirst, rect, cornerRadius);
	}
	else{
		fillRect(dwFirst, rect);
	}
}

void GdiPaint::fillPath(Long dwPenColor){
}

void GdiPaint::fillPie(Long dwPenColor, const FCRect& rect, float startAngle, float sweepAngle){
}

void GdiPaint::fillPolygon(Long dwPenColor, FCPoint *apt, int cpt){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if(dwPenColor < 0 ) dwPenColor = -dwPenColor / 1000;
	for(int i = 0; i< cpt; i++){
		int x = apt[i].x + m_offsetX;
		int y = apt[i].y + m_offsetY;
		if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
			x = (int)(m_scaleFactorX * x);
			y = (int)(m_scaleFactorY * y);
		}
		apt[i].x = x;
		apt[i].y = y;
	}
	HBRUSH brush = ::CreateSolidBrush((int)dwPenColor);
	::SelectObject(m_hDC, brush);
	::Polygon(m_hDC, apt, cpt);
	::DeleteObject(brush);
}

void GdiPaint::fillRect(Long dwPenColor, const FCRect& rect){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if(dwPenColor < 0 ) dwPenColor = -dwPenColor / 1000;
	FCRect newRect = {rect.left + m_offsetX, rect.top + m_offsetY, rect.right + m_offsetX, rect.bottom + m_offsetY};
	affectScaleFactor(&newRect);
	HBRUSH brush = ::CreateSolidBrush((int)dwPenColor);
	::SelectObject(m_hDC, brush);
	::FillRect(m_hDC, &newRect, brush);
	::DeleteObject(brush);
}

void GdiPaint::fillRect(Long dwPenColor, int left, int top, int right, int bottom){
	FCRect newRect = {left, top, right, bottom};
	fillRect(dwPenColor, newRect);
}

void GdiPaint::fillRoundRect(Long dwPenColor, const FCRect& rect, int cornerRadius){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if(dwPenColor < 0 ) dwPenColor = -dwPenColor / 1000;
	FCRect newRect = {rect.left + m_offsetX, rect.top + m_offsetY, rect.right + m_offsetX, rect.bottom + m_offsetY};
	affectScaleFactor(&newRect);
	HBRUSH brush = ::CreateSolidBrush((int)dwPenColor);
	::SelectObject(m_hDC, brush);
	if(cornerRadius != 0){
		::RoundRect(m_hDC, newRect.left, newRect.top, newRect.right, newRect.bottom, cornerRadius, cornerRadius);
	}
	else{
		::FillRect(m_hDC, &newRect, brush);
	}
	::DeleteObject(brush);
}

Long GdiPaint::getColor(Long dwPenColor){
	return m_myColor->getUserColor(dwPenColor);
}

Long GdiPaint::getPaintColor(Long dwPenColor){
	return getColor(dwPenColor);
}

FCPoint GdiPaint::getOffset(){
	FCPoint offset = {m_offsetX, m_offsetY};
	return offset;
}

void GdiPaint::getScaleFactor(double *scaleFactorX, double *scaleFactorY){
	*scaleFactorX = m_scaleFactorX;
	*scaleFactorY = m_scaleFactorY;
}

FCPoint GdiPaint::rotate(const FCPoint& op, const FCPoint& mp, int angle){
	FCPoint pt = {0};
	return pt;
}

void GdiPaint::setClip(const FCRect& rect){
	if(m_hRgn){
		DeleteObject(m_hRgn);
	}
	FCRect newRect = {rect.left + m_offsetX, rect.top + m_offsetY, rect.right + m_offsetX, rect.bottom + m_offsetY};
	if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
        newRect.left = (int)floor(newRect.left * m_scaleFactorX);
        newRect.top = (int)floor(newRect.top * m_scaleFactorY);
        newRect.right = (int)ceil(newRect.right * m_scaleFactorX);
        newRect.bottom = (int)ceil(newRect.bottom * m_scaleFactorY);
    }
	m_hRgn = ::CreateRectRgnIndirect(&newRect);
	::SelectClipRgn(m_hDC, m_hRgn);
}

void GdiPaint::setLineCap(int startLineCap, int endLineCap){
}

void GdiPaint::setOffset(const FCPoint& offset){
	m_offsetX = offset.x;
	m_offsetY = offset.y;
}

void GdiPaint::setOpacity(float opacity){
}

void GdiPaint::setResourcePath(const String& resourcePath){
	m_resourcePath = resourcePath;
}

void GdiPaint::setRotateAngle(int rotateAngle){
	m_rotateAngle = rotateAngle;
}

void GdiPaint::setScaleFactor(double scaleFactorX, double scaleFactorY){
	m_scaleFactorX = scaleFactorX;
	m_scaleFactorY = scaleFactorY;
}

bool GdiPaint::supportTransparent(){
	return false;
}

FCSize GdiPaint::textSize(String strText, FCFont *font, int width){
	FCSize size = { 0 };
	String fontFamily = font->m_fontFamily;
	if(fontFamily == L"Default"){
		fontFamily = L"Segoe UI";
	}
	float fontSize = font->m_fontSize + 6;
	double rate = 1;
	if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
		rate = (m_scaleFactorX + m_scaleFactorY) / 2;
		fontSize = (float)(fontSize * (m_scaleFactorX + m_scaleFactorY) / 2);
	}
	HFONT hFont = CreateFont
	(
		fontSize, 0,    
		0, 0,    
		font->m_bold? FW_BOLD: FW_REGULAR,    
		font->m_italic ? 1: 0,
		font->m_underline? 1: 0,
				0,
				GB2312_CHARSET,
				OUT_DEFAULT_PRECIS,
				CLIP_DEFAULT_PRECIS,
				DEFAULT_QUALITY,
				DEFAULT_PITCH | FF_SWISS, 
		fontFamily.c_str()    
	);
	HFONT hOldFont = (HFONT)::SelectObject(m_hDC, hFont);
	::GetTextExtentPoint32(m_hDC, strText.c_str(), (int)strText.length(), &size);
	size.cx = (int)(size.cx / rate);
	size.cy = (int)(size.cy / rate);
	::SelectObject(m_hDC, hOldFont);
	::DeleteObject(hFont);
	return size;
}

FCSizeF GdiPaint::textSizeF(String strText, FCFont *font, int width){
	FCSize size = textSize(strText, font, width);
	FCSizeF fSize = {(float)size.cx, (float)size.cy};
	return fSize;
}